package yxy.game.pm2.command.pickedtroops;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PickedTroopsSlot;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PickedTroopsMessage;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.pickedtroops.PickedTroopsHandle;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;

import java.util.ArrayList;

/****************************************
 * @name CMD_PickedTroopsSlotRecoin
 * @description 兵符重铸保存
 * @author 梁健彬
 * @since 2022-05-20
 ***************************************/
@MessageType(proto = Command.PickedTroopsSlotRecoin, classtype = PickedTroopsMessage.PickedTroopsSlotRecoinRequest.class)
public class CMD_PickedTroopsSlotRecoin extends Command<PickedTroopsMessage.PickedTroopsSlotRecoinRequest> {

    @Override
    public void execute(MessageSource messageSource, PickedTroopsMessage.PickedTroopsSlotRecoinRequest request) {
        String playerCode = messageSource.getTag();
        String slotCode = request.getSlotCode();//兵符的code
        int isChangePower = request.getIsChangePower();//0 不是  1 是

        PickedTroopsMessage.PickedTroopsSlotRecoinResponse.Builder response = PickedTroopsMessage.PickedTroopsSlotRecoinResponse.newBuilder();
        try {
            if (PickedTroopsSlot.lockPickedTroopsSlot(playerCode)) {

                //判断兵符是否存在
                PickedTroopsSlot slot = PickedTroopsSlot.get(playerCode, slotCode);
                if (slot == null) {
                    send(messageSource, response, Result.ParamError);
                    return;
                }

                //改兵符的词条缓存
                ArrayList<Integer> reEntryId = slot.getReEntryId();//重置的词条
                ArrayList<Integer> reValue = slot.getReValue();//重置的词条值
                slot.setEntryId(reEntryId);
                slot.setValue(reValue);
                slot.setReEntryId(new ArrayList<Integer>());
                slot.setReValue(new ArrayList<Integer>());
                PickedTroopsSlot.set(playerCode, slot);
                //返回兵符信息
                response.setSlot(PickedTroopsHandle.getSlot(slot));

                //更新战力
                if (isChangePower != 0) {
                    PlayTypeHandle.savePickedTroop(playerCode, PlayTypeModule.get(playerCode));
                    new BattleHandle().powerUpdate(playerCode);// 战力更新
                }

                send(messageSource, response, Result.Success);
            }
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        } finally {
            PickedTroopsSlot.unlockPickedTroopsSlot(playerCode);
        }


    }

    private void send(MessageSource messageSource, PickedTroopsMessage.PickedTroopsSlotRecoinResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}